﻿//2014-01-06
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Service.BasicDataManage;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.BasicDataManage
{
    public class ProductBusiness
    {
        private readonly ProductService _dal = new ProductService();

        public string GetProductList(ProductQcEntity qcmodel)
        {
            var total = _dal.GetProductCount(qcmodel);
            var list = _dal.GetProductList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetProductListByCombobox()
        {
            return JsonHelper.Encode(_dal.GetProductListByCombobox());
        }

        public string GetProductListByQuery(string queryPrdCode)
        {
            return JsonHelper.Encode(_dal.GetProductListByQuery(queryPrdCode));
        }

        public void AddProduct(string data)
        {
            var model = new ProductEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var flowList = new List<FlowEntity>();
            var flowDocList = new List<FlowDocEntity>();
            foreach (Hashtable row in rows)
            {
                model.PrdID = Guid.NewGuid();
                model.PrdCode = row["PrdCode"].ToString();
                model.PrdName = row["PrdName"].ToString();
                model.PrdDesc = row["PrdDesc"].ToString();
                model.CheckFile = row["CheckFile"].ToString();
                model.OpFile = row["OpFile"].ToString();
                model.RecStatus = true;
                model.ArrFlowList = (ArrayList)row["arrFlowList"];
                model.ArrJyList = (ArrayList)row["arrJyList"];
                model.ArrZyList = (ArrayList)row["arrZyList"];
            }
            if (model.ArrFlowList.Count > 0)
            {
                foreach (Hashtable i in model.ArrFlowList)
                {
                    var flowmodel = new FlowEntity();
                    flowmodel.FlowID = Guid.NewGuid();
                    flowmodel.PrdID = model.PrdID;
                    flowmodel.ProcessIdx = int.Parse(i["ProcessIdx"].ToString());
                    flowmodel.ModelMin = Convert.ToDecimal(i["ModelMin"].ToString());
                    flowList.Add(flowmodel);
                    foreach (Hashtable j in model.ArrJyList)
                    {
                        if (flowmodel.ProcessIdx != int.Parse(j["ProcessIdx"].ToString())) continue;
                        var flowDocmodel = new FlowDocEntity();
                        flowDocmodel.DocID = Guid.NewGuid();
                        flowDocmodel.FlowID = flowmodel.FlowID;
                        flowDocmodel.PageNo = Convert.ToInt32(j["PageNo"].ToString());
                        flowDocmodel.DocType = 0;
                        flowDocList.Add(flowDocmodel);
                    }
                    foreach (Hashtable k in model.ArrZyList)
                    {
                        if (flowmodel.ProcessIdx != int.Parse(k["ProcessIdx"].ToString())) continue;
                        var flowDocmodel = new FlowDocEntity();
                        flowDocmodel.DocID = Guid.NewGuid();
                        flowDocmodel.FlowID = flowmodel.FlowID;
                        flowDocmodel.PageNo = Convert.ToInt32(k["PageNo"].ToString());
                        flowDocmodel.DocType = 1;
                        flowDocList.Add(flowDocmodel);
                    }
                }
                _dal.AddProduct(model);
                _dal.BatchFlow(flowList);
                _dal.BatchFlowDoc(flowDocList);
            }
            else
            {
                _dal.AddProduct(model);
            }
        }

        public void ModifyProduct(string data)
        {
            var model = new ProductEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var flowList = new List<FlowEntity>();
            var flowDocList = new List<FlowDocEntity>();
            foreach (Hashtable row in rows)
            {
                model.PrdID = new Guid(row["PrdID"].ToString());
                model.PrdCode = row["PrdCode"].ToString();
                model.PrdName = row["PrdName"].ToString();
                model.PrdDesc = row["PrdDesc"].ToString();
                model.CheckFile = row["CheckFile"].ToString();
                model.OpFile = row["OpFile"].ToString();
                model.ArrFlowList = (ArrayList)row["arrFlowList"];
                model.ArrJyList = (ArrayList)row["arrJyList"];
                model.ArrZyList = (ArrayList)row["arrZyList"];
            }

            if (model.ArrFlowList.Count > 0)
            {
                foreach (Hashtable i in model.ArrFlowList)
                {
                    var flowmodel = new FlowEntity();
                    var flowid = i["FlowID"].ToString();
                    flowmodel.FlowID = flowid.Equals("00000000-0000-0000-0000-000000000000") ? Guid.NewGuid() : new Guid(flowid);
                    flowmodel.PrdID = model.PrdID;
                    flowmodel.ProcessIdx = int.Parse(i["ProcessIdx"].ToString());
                    flowmodel.ModelMin = Convert.ToDecimal(i["ModelMin"].ToString());
                    flowList.Add(flowmodel);
                    foreach (Hashtable j in model.ArrJyList)
                    {
                        if (flowmodel.ProcessIdx != int.Parse(j["ProcessIdx"].ToString())) continue;
                        var flowDocmodel = new FlowDocEntity();
                        flowDocmodel.DocID = Guid.NewGuid();
                        flowDocmodel.FlowID = flowmodel.FlowID;
                        flowDocmodel.PageNo = Convert.ToInt32(j["PageNo"].ToString());
                        flowDocmodel.DocType = 0;
                        flowDocList.Add(flowDocmodel);
                    }
                    foreach (Hashtable k in model.ArrZyList)
                    {
                        if (flowmodel.ProcessIdx != int.Parse(k["ProcessIdx"].ToString())) continue;
                        var flowDocmodel = new FlowDocEntity();
                        flowDocmodel.DocID = Guid.NewGuid();
                        flowDocmodel.FlowID = flowmodel.FlowID;
                        flowDocmodel.PageNo = Convert.ToInt32(k["PageNo"].ToString());
                        flowDocmodel.DocType = 1;
                        flowDocList.Add(flowDocmodel);
                    }
                }
                var list = _dal.GetAllFlowList(model.PrdID);
                var addlist =
                    list.Union(flowList, new FlowComparer()).Except(list.Intersect(flowList, new FlowComparer()),
                                                                   new FlowComparer()).ToList();

                var updatelist =
                    list.Union(flowList, new FlowComparer2()).Except(list.Intersect(flowList, new FlowComparer2()),
                                                                   new FlowComparer2()).ToList();
                foreach (var i in flowList)
                {
                    _dal.DeleteFlowDoc(i.FlowID);
                }
                _dal.ModifyProduct(model);
                if (addlist.Count > 0)
                {
                    _dal.BatchFlow(addlist);
                }
                if (updatelist.Count > 0)
                {
                    _dal.ModifyFlow(updatelist);
                }
                _dal.BatchFlowDoc(flowDocList);
            }
            else
            {
                _dal.ModifyProduct(model);
            }
        }

        public string GetProductInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetProductInfo(id));
        }

        public string GetFlowListById(ProductQcEntity qcmodel)
        {
            //var total = _dal.GetFlowListByIdCount(qcmodel);
            var list1 = _dal.GetFlowListById(qcmodel);
            var list2 = _dal.GetFlowDocListById();
            var list = new List<FlowEntity>();
            if (list1.Count > 0)
            {
                foreach (var i in list1)
                {
                    var model = new FlowEntity();
                    model.FlowID = i.FlowID;
                    model.ProcessIdx = i.ProcessIdx;
                    model.ModelMin = i.ModelMin;
                    var jyItem = new List<FlowDocEntity>();
                    var zyItem = new List<FlowDocEntity>();
                    foreach (var j in list2.Where(j => i.FlowID == j.FlowID))
                    {
                        if (j.DocType == 0)
                        {
                            jyItem.Add(j);
                        }
                        else
                        {
                            zyItem.Add(j);
                        }
                    }
                    model.JyItems = jyItem;
                    model.ZyItems = zyItem;
                    list.Add(model);
                }
            }
            return JsonHelper.Encode(list);
        }

        //public string GetFlowDocListById(FlowDocQcEntity qcmodel)
        //{
        //    var list = _dal.GetFlowDocListById(qcmodel);
        //    return JsonHelper.Encode(list);
        //}

        public string GetFlowInfo(ProductQcEntity qcmodel)
        {
            //查询工序
            var list = _dal.GetFlowInfo(qcmodel);
            //查询页数
            var itemlist = _dal.GetFlowDocListByPrdId(qcmodel.PrdID);
            foreach (var t in list)
            {
                var jyitem = new List<FlowDocEntity>();
                var zyitem = new List<FlowDocEntity>();
                foreach (var j in itemlist)
                {
                    if (t.FlowID == j.FlowID)
                    {
                        if (j.DocType == 0)
                        {
                            jyitem.Add(j);
                            t.JyItems = jyitem;
                        }
                        else
                        {
                            zyitem.Add(j);
                            t.ZyItems = zyitem;
                        }
                    }
                }
            }
            return JsonHelper.Encode(list);
        }
    }
}
